package org.apache.solr.search.similarities;

import org.apache.lucene.search.similarities.Distribution; // javadoc
import org.apache.lucene.search.similarities.DistributionLL;
import org.apache.lucene.search.similarities.DistributionSPL;
import org.apache.lucene.search.similarities.IBSimilarity;
import org.apache.lucene.search.similarities.Lambda;
import org.apache.lucene.search.similarities.LambdaDF;
import org.apache.lucene.search.similarities.LambdaTTF;
import org.apache.lucene.search.similarities.Normalization;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.schema.SimilarityFactory;

/**
 * Factory for {@link IBSimilarity}
 * <p>
 * You must specify the implementations for all three components of the
 * Information-Based model (strings).
 * <ol>
 *     <li>{@link Distribution distribution}: Probabilistic distribution used to
 *         model term occurrence
 *         <ul>
 *             <li>{@link DistributionLL LL}: Log-logistic</li>
 *             <li>{@link DistributionLL SPL}: Smoothed power-law</li>
 *         </ul>
 *     </li>
 *     <li>{@link Lambda lambda}: &lambda;<sub>w</sub> parameter of the
 *         probability distribution
 *         <ul>
 *             <li>{@link LambdaDF DF}: <code>N<sub>w</sub>/N</code> or average
 *                 number of documents where w occurs</li>
 *             <li>{@link LambdaTTF TTF}: <code>F<sub>w</sub>/N</code> or
 *                 average number of occurrences of w in the collection</li>
 *         </ul>
 *     </li>
 *     <li>{@link Normalization normalization}: Term frequency normalization 
 *         <blockquote>Any supported DFR normalization listed in
                       {@link DFRSimilarityFactory}</blockquote>
       </li>
 * </ol>
 * <p>
 * Optional settings:
 * <ul>
 *   <li>discountOverlaps (bool): Sets
 *       {@link IBSimilarity#setDiscountOverlaps(boolean)}</li>
 * </ul>
 * @lucene.experimental
 */

public class IBSimilarityFactory extends SimilarityFactory {

    private boolean discountOverlaps;
    private Distribution distribution;
    private Lambda lambda;
    private Normalization normalization;

    @Override
    public void init(SolrParams params) {
        super.init(params);

        discountOverlaps = params.getBool("discountOverlaps", true);
        distribution = parseDistribution(params.get("distribution"));
        lambda = parseLambda(params.get("lambda"));
        normalization = DFRSimilarityFactory.parseNormalization(params.get("normalization"), params.get("c"), params.get("mu"), params.get("z"));
    }

    private Distribution parseDistribution(String expr) {

        switch (expr) {
            case "LL"   : return new DistributionLL();
            case "SPL"  : return new DistributionSPL();
            default     : throw new RuntimeException("Invalid distribution: " + expr);
        }
    }

    private Lambda parseLambda(String expr) {

        switch (expr) {
            case "DF"   : return new LambdaDF();
            case "TTF"  : return new LambdaTTF();
            default     : throw new RuntimeException("Invalid lambda: " + expr);
        }
    }

    @Override
    public Similarity getSimilarity() {

        IBSimilarity sim = new IBSimilarity(distribution, lambda, normalization);
        sim.setDiscountOverlaps(discountOverlaps);

        return sim;
    }
}
